//void, Obj This

Catapult this;//the hero of this story
int emptytimestartdamage, maxunits, minunits, buildspeed, decayspeed; //building constants taken from const.ini
int cycletime; //how much time it takes to run the building loop (constant)
int emptytime, healthleft, unitsinside;//variables
int origsight;

this = This.AsCatapult();

if(!.IsValid) return;
if(!.IsAlive) return;

cycletime = 500;
Sleep(rand(10)+30);
if(!.IsBuilt)
{//catapult building

	//fundamental constants init
	origsight = .sight;
	.SetSight(GetConst("CatapultBuildSight"));
	emptytimestartdamage = GetConst("CatapultBuildOnEmptyStartDamageTime");
	maxunits             = GetConst("CatapultMaxUnits");
	minunits             = GetConst("CatapultBuildUnits");
	buildspeed           = GetConst("CatapultBuildSpeed");
	decayspeed           = GetConst("CatapultDecaySpeed");

	//variables init
	emptytime = 0;
	healthleft = .maxhealth - 1;

	.StartDelayedAnim(1, .pos, -1);
	while(.IsAlive)
	{
		if (.IsBuilt) break;
		unitsinside = .settlement.UnitsCount();
		if(unitsinside > 0)
		{
			if(unitsinside >= minunits)
			{
				//ObjList olLF;
				int healamount;

				//olLF = .settlement.Units.ObjClass("LightInfantry");
				//olLF.ClearDead();
				emptytime = 0;
				//healamount = .maxhealth * (buildspeed+olLF.count) * unitsinside / (maxunits * 100);//percents of .maxhealth to heal, per second
				healamount = .maxhealth * buildspeed * unitsinside / (maxunits * 100);//percents of .maxhealth to heal, per second

				if(healthleft > healamount)
				{
					healthleft -= healamount;
					.Heal(healamount);
					.SetBuildFrame(healthleft);
				}
				else
				{
					.Heal(healthleft);
					healthleft = 0;
					.SetBuilt;
				}
			}
		}
		else
		{//no units inside
			int damageamount;
			damageamount = (.maxhealth * decayspeed * cycletime) / 100000;// 100000 is 100(damagespeed is percents) * 1000(which is one second)

			.SetBuildFrame(healthleft);

			emptytime += cycletime;
			if(emptytime > emptytimestartdamage)
			{
				.Damage(damageamount);
				healthleft += damageamount;
				if(healthleft > .maxhealth) healthleft = .maxhealth;
			}
		}
		Sleep(cycletime);
	}

	.SetSight(origsight);
	
	if (.race == Britain) {
		//.Face(.pos + Point(-3, -2));
		Sleep(rand(10)+30);
		.SetCommand("disband");
		return;
	}

	
	if (.race == Germany) {
		Sleep(rand(10)+30);
		.SetCommand("disband");
		return;
	}

	ExploreCircle(.player, .pos, .sight);
	
	.AddCommand(true, "autofire");
} else {
	if(.settlement.UnitsCount == 0) {
		if (.race == Britain || .race == Germany) {
			if (.IsAlive()) {
				Unit unit;
				str class_name;
				if (.race == Britain) class_name = "BCatapultUnit";
				else if (.race == Germany) class_name = "TCatapultUnit";
				Sleep(rand(100)+100);
				unit = Place(class_name, .pos, .player);
				if(unit.IsValid){
					unit.SetHealth(.health);
					unit.Face(unit.pos + Point(-3,-2));
					SwapSelectedObj(this, unit);
					.Erase();
				}
			}
		} else
			.Damage(100000);
	}
	else emptytime = 0;
	Sleep(cycletime);
}